/**
 * Created by Yixin on 2017/8/19.
 */
define([
    "app"
], function (app) {
    app.controller("manageRegisterController", [
        "$scope",
        "manageRegisterDataService",
        "$uibModal",
        function ($scope, manageRegisterDataService, $uibModal) {

            // 页面加载状态
            $scope.state = "loading";

            manageRegisterDataService
                .getAllRegistration()
                .then(function success(resp) {
                    // 设置页面加载完毕
                    $scope.state = "finished";

                    var teams = resp.data;

                    $scope.pagination = {
                        currentPage: 1,// 当前页码
                        maxPageSize: 5,// 分页按钮的最多个数,多了会隐藏
                        totalItems: teams.length,// 项目总数
                        itemsPerPage: 8 // 每页的项目数
                    };
                    // $scope.currentPage = 1;
                    // $scope.maxPageSize = 5;
                    // $scope.totalItems = teams.length;
                    // $scope.itemsPerPage = 6;
                    $scope.currentCate = "全部";

                    // 当前筛选条件下的元素
                    var teamInCondition = teams;

                    // 统计每种状态的团队数
                    (function () {
                        $scope.allTeamNumber = teams.length;
                        $scope.nonAuditNumber = $scope.rejectNumber = $scope.agreeNumber = 0;
                        teams.forEach(function (e) {
                            switch (e.auditOpinion) {
                                case "未审核":
                                    $scope.nonAuditNumber++;
                                    break;
                                case "已同意":
                                    $scope.agreeNumber++;
                                    break;
                                case "已拒绝":
                                    $scope.rejectNumber++;
                                    break;
                            }
                        })
                    })();

                    //设置页面的项目
                    function setPageItems() {
                        return teamInCondition.slice(($scope.pagination.currentPage - 1) * $scope.pagination.itemsPerPage, $scope.pagination.currentPage * $scope.pagination.itemsPerPage);
                    }

                    // 在页面中的元素
                    $scope.teamInPage = setPageItems();

                    // 页面切换
                    $scope.pageChange = function () {
                        console.log("page switch to " + $scope.pagination.currentPage);
                        console.log($scope);
                        $scope.teamInPage = setPageItems();
                    };

                    // 切换筛选条件
                    $scope.switchFilter = function (keyword) {
                        console.log(keyword);
                        $scope.currentCate = keyword;
                        switch (keyword) {
                            case "全部":
                                teamInCondition = teams;
                                break;
                            default:
                                teamInCondition = teams.filter(function (item) {
                                    return item.auditOpinion == keyword;
                                });
                        }
                        $scope.pagination.currentPage = 1;
                        $scope.pageChange();
                        $scope.pagination.totalItems = teamInCondition.length;
                    };

                    // 查看团队的详细信息
                    $scope.checkDetails = function (team) {
                        $uibModal.open({
                            animation: true,
                            backdrop: "static",
                            controller: "manageRegisterDialogController",
                            templateUrl: "manage-register-dialog.html",
                            size: "lg",
                            resolve: {
                                team: team
                            }
                        });
                    }
                }, function error() {
                    $scope.state = "error";
                });

        }]);
});